$(function() {
    $(".loadingMarsk").css({
        "width":window.screen.availWidth,
        "height":window.screen.availHeight,
    });
    $(".mobile").html(localStorage.userMobile);
    //获取验证码
    var token = localStorage.token;
    if(!token) {
        location.href = "../loginAndReg/login.html";
        return false;
    }
    new FastClick(document.body);
    var flag = false;
    $(".code").on("click",function() {
        if(flag) return false;
        var mobile = localStorage.userMobile;
        $.ajax({
            type: "post",
            url: domain.test + "/gettelcode",
            // url: domain.online + "/captcha",
            data: {
                mobile:mobile
            },
            beforeSend: function (xhr) {
                $(".loadingMarsk").css("background-color","rgba(0,0,0,.3)");
                $(".loadingMarsk").css("display","block");
                xhr.setRequestHeader("token",token);
            },
            success: function (response) {
                if(response.data.rtnCode===200) {
                // if(response.rtnCode===200) {
                    var counter = 90;
                    var timer = setInterval(function() {
                        counter--;
                        if(counter===0) {
                            $(".code").html("获取短息验证码");
                            clearInterval(timer);
                        }else {
                            $(".code").html(counter + "s");
                            flag = true;
                        }
                    },1000);
                }
                $(".loadingMarsk").css("display","none");
            }
        });
    });
    //修改支付密码
    function tips(tips) {
        $(".tips").css("display", "block").html(tips).animate({
            "opacity": 1
        }, 1000, function () {
            setTimeout(function () {
                $(".tips").animate({
                    "opacity": 0
                }, 1000, function () {
                    $(".tips").css("display", "none");
                })
            }, 1000);
        })
    }
    $(".submitBtn").on("click",function() {
        var code = parseInt($(".codeNum").val());
        if(!code) {
            tips("请输入验证码");
            return false;
        }
        var logPsd = $(".logPsd").val();
        if(!logPsd) {
            tips("请输入登录密码");
            return false;
        }
        var payPsd = $(".newPsd").val();
        if(!payPsd) {
            tips("请输入新的支付密码");
            return false;
        }
        if(payPsd.length<6||payPsd.length>6) {
            tips("支付密码由6位数字组成");
            return false;
        }
        $.ajax({
            type: "post",
            url: domain.test + "/resetpaypsd",
            // url: domain.online + "/member/payPwd",
            data: {
                password:logPsd,
                payPassword:payPsd,
                captcha:code
            },
            beforeSend: function (xhr) {
                $(".loadingMarsk").css("background-color","rgba(0,0,0,.3)");
                $(".loadingMarsk").css("display","block");
                xhr.setRequestHeader("token",token);
            },
            success: function (response) {
                if(response.data.rtnCode===200) {
                // if(response.rtnCode===200) {
                    tips("修改成功");
                    $(".loadingMarsk").css("display","none");
                    setTimeout(function(){
                        history.back();
                    },1000);
                }
                if(response.data.rtnCode===400) {
                // if(response.rtnCode===400) {
                    tips(response.data.msg);
                    // tips(response.msg);
                }
                if(response.data.rtnCode===401) {
                // if(response.rtnCode===401) {
                    var outTimeCode = response.data.rtnCode;
                    // var outTimeCode = response.rtnCode;
                    location.href = "../../index.html?outTimeCode=" + outTimeCode;
                }
            }
        });
    });
});